package net.cl.utils;

import java.util.regex.Matcher;

import net.cl.Provider;
import net.cl.tools.Validator;

/**
 * 字符串处理工具类
 * 
 * @author CaoLei
 */
public class StringUtil {

	public static final String EMPTY = "";
	public static final String SPACE = " ";
	public static final String UTF8 = "UTF-8";
	public static final String NULL = "null";

	/**
	 * 删除字符串中所有空格和回车
	 */
	public static String clearWhitespace(String input) {
		if (input == null)
			return NULL;
		StringBuilder sb = new StringBuilder();
		char c;
		for (int i = 0; i < input.length(); i++) {
			c = input.charAt(i);
			if (!Character.isWhitespace(c))
				sb.append(c);
		}
		return sb.toString();
	}

	/**
	 * 删除字符串最后一个字符
	 */
	public static String deleteLast(String input) {
		if (Validator.isEmpty(input))
			return EMPTY;
		else
			return input.substring(0, input.length() - 1);
	}

	/**
	 * 字符串里的空格格式化为1个标准空格
	 */
	public static String formatWhiteSpace(String in) {
		if (in == null)
			return NULL;
		StringBuilder sb = new StringBuilder(in);
		boolean flag = false;
		char c;
		for (int i = sb.length() - 1; i >= 0; i--) {
			c = sb.charAt(i);
			if (Character.isWhitespace(c) && flag)
				sb.deleteCharAt(i);
			if (Character.isWhitespace(c)) {
				sb.setCharAt(i, ' ');
				flag = true;
			} else
				flag = false;
		}
		return sb.toString();
	}

	/**
	 * 左边补字符以满足长度要求
	 */
	public static String addLeft(String str, char c, int resultLength) {
		if (str == null)
			str = EMPTY;
		StringBuilder sb = new StringBuilder(str);
		while (sb.length() < resultLength)
			sb.insert(0, c);
		return sb.toString();
	}

	/**
	 * 如果obj是null值,显示为""
	 */
	public static String nullValue(Object obj) {
		return obj == null ? EMPTY : obj.toString();
	}

	/**
	 * sub在str中出现的次数
	 */
	public static int countMatches(String str, String sub) {
		if (Validator.isEmpty(str) || Validator.isEmpty(sub))
			return 0;
		int count = 0;
		int idx = 0;
		while ((idx = str.indexOf(sub, idx)) != -1) {
			count++;
			idx += sub.length();
		}
		return count;
	}

	/**
	 * 驼峰命名法<br>
	 * '_','-','@','$','#',' '将被视作分隔符
	 * 
	 * @param firstUpper
	 *            首字母是否大写
	 */
	public static String getCamelCase(String inputString, boolean firstUpper) {
		StringBuilder sb = new StringBuilder();
		boolean nextUpperCase = false;
		for (int i = 0; i < inputString.length(); i++) {
			char c = inputString.charAt(i);
			switch (c) {
			case '_':
			case '-':
			case '@':
			case '$':
			case '#':
			case ' ':
				if (sb.length() > 0)
					nextUpperCase = true;
				break;
			default:
				if (nextUpperCase) {
					sb.append(Character.toUpperCase(c));
					nextUpperCase = false;
				} else
					sb.append(Character.toLowerCase(c));
				break;
			}
		}
		if (firstUpper)
			sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * 判断一个字符是Ascii字符还是其它字符(如汉，日，韩文字符)
	 */
	public static boolean isAscii(char c) {
		int k = 0x80;
		return c / k == 0;
	}

	/**
	 * 将输入字符串的首字母变为小写
	 */
	public static String firstLower(String s) {
		if (Validator.isEmpty(s))
			return EMPTY;
		StringBuilder sb = new StringBuilder(s);
		sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * 将输入字符串的首字母变为大写
	 */
	public static String firstUpper(String s) {
		if (Validator.isEmpty(s))
			return EMPTY;
		StringBuilder sb = new StringBuilder(s);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		return sb.toString();
	}

	public static String join(String[] array, String separator, int start, int end) {
		if (array == null || array.length == 0)
			return NULL;
		StringBuilder sb = new StringBuilder();
		for (int i = start, j = 0; i < end && i < array.length; i++, j++) {
			if (j > 0)
				sb.append(separator);
			sb.append(array[i]);
		}
		return sb.toString();
	}

	public static String join(String[] array, String separator) {
		if (array == null || array.length == 0)
			return NULL;
		return join(array, separator, 0, array.length);
	}

	public static String getMatch(String pattern, String str, int index) {
		if (pattern == null || str == null)
			throw new NullPointerException();
		Matcher matcher = Provider.getPattern(pattern).matcher(str);
		if (matcher.find())
			return matcher.group(index);
		return null;
	}

	public static String getMatch(String pattern, String str) {
		return getMatch(pattern, str, 1);
	}

}
